/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.potentialadmingroup.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.knet.sp.ncrm.common.service.ServiceException;
import cn.knet.sp.ncrm.common.service.TreeService;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.modules.customer.potentialadmingroup.dao.PotentialAdminGroupDao;
import cn.knet.sp.ncrm.modules.customer.potentialadmingroup.entity.PotentialAdminGroup;

/**
 * 管理员分组Service
 *
 * @author 葛海洋
 * @version 2015-08-25
 */
@Service
@Transactional(readOnly = true)
public class PotentialAdminGroupService extends TreeService<PotentialAdminGroupDao, PotentialAdminGroup> {

	@Resource
	private PotentialAdminGroupDao potentialAdminGroupDao;
	
	public PotentialAdminGroup get(String id) {
		return super.get(id);
	}

	public List<PotentialAdminGroup> findList(PotentialAdminGroup potentialAdminGroup) {
		List<PotentialAdminGroup> adminGroups = super.findList(potentialAdminGroup);
		//默认分组
		PotentialAdminGroup defaultAdminGroup = new PotentialAdminGroup();
		defaultAdminGroup.setId("default");
		defaultAdminGroup.setParent(null);
		defaultAdminGroup.setName("未分组");
		defaultAdminGroup.setVersion(1L);
		adminGroups.add(0, defaultAdminGroup);
		return adminGroups;
	}

	@Transactional(readOnly = false)
	public void save(PotentialAdminGroup potentialAdminGroup) {
		//判断父级是否【未分组】
		if (potentialAdminGroup.getParentId().equals("default")) {
			throw new ServiceException("【未分组】不能添加下级分组");
		}
		//判断是否存在同名分组
		PotentialAdminGroup tempAdminGroup = getByName(potentialAdminGroup.getName());
		if( tempAdminGroup != null ) {
			if (!tempAdminGroup.getId().equals(potentialAdminGroup.getId())) {
				throw new ServiceException("存在同名的分组，添加失败");
			}
		}
		//如何parentId等于0，则为顶级分组，level设置为1
		if (potentialAdminGroup.getParentId().equals("0")) {
			potentialAdminGroup.setLevelNum(1);
		} else {
			PotentialAdminGroup parentAdminGroup = get(potentialAdminGroup.getParentId());
			if (parentAdminGroup != null && StringUtils.isNotBlank(parentAdminGroup.getId())) {
				Integer levelNum = parentAdminGroup.getLevelNum();
				//管理员分组限制3级
				if (levelNum == 3) {
					throw new ServiceException("最多只能添加3级分组，添加失败");
				}
				potentialAdminGroup.setLevelNum(parentAdminGroup.getLevelNum() + 1);
			} else {
				throw new ServiceException("选择的上级分组已经被删除");
			}
		}
		Date nowDate = new Date();
		if (StringUtils.isBlank(potentialAdminGroup.getId())) {
			potentialAdminGroup.setDateCreated(nowDate);
			potentialAdminGroup.setLastUpdated(nowDate);
		} else {
			potentialAdminGroup.setLastUpdated(nowDate);
		}
		super.save(potentialAdminGroup);
	}

	@Transactional(readOnly = false)
	public void delete(PotentialAdminGroup potentialAdminGroup) {
		super.delete(potentialAdminGroup);
	}

	/**
	 *  通过分组名称获取分组信息
	 * @author Gehaiyang
	 * @date 2015年9月15日 上午9:54:59
	 * @param groupName
	 * @return
	 */
	public PotentialAdminGroup getByName(String groupName) {
		PotentialAdminGroup potentialAdminGroup = new PotentialAdminGroup();
		potentialAdminGroup.setName(groupName);
		return potentialAdminGroupDao.getByName(potentialAdminGroup);
	}

}